package br.com.ss.seguranca.model.entity;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * User generated by hbm2java
 */
@Entity
@Table(name = "user", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
@NamedQueries({ 
	@NamedQuery(name = "User.findByUsername", query = "SELECT usu FROM User usu WHERE usu.username = :username")
	})
public class User implements java.io.Serializable, UserDetails {

	private Integer id;
	private String password;
	private String username;
	private Set<UserRole> userRoles = new HashSet<UserRole>(0);

	public User() {
	}

	public User(String password, String username) {
		this.password = password;
		this.username = username;
	}

	public User(String password, String username, Set<UserRole> userRoles) {
		this.password = password;
		this.username = username;
		this.userRoles = userRoles;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "password", nullable = false, length = 20)
	@NotNull
	@Length(max = 20)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "username", unique = true, nullable = false, length = 20)
	@NotNull
	@Length(max = 20)
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
	public Set<UserRole> getUserRoles() {
		return this.userRoles;
	}

	public void setUserRoles(Set<UserRole> userRoles) {
		this.userRoles = userRoles;
	}


	  @Transient
	  public Collection<GrantedAuthority> getAuthorities() {
	    List<GrantedAuthority> result = new ArrayList<GrantedAuthority>();
//	    for (UserRole ur : userRoles) {
//	      result.add(new GrantedAuthorityImpl(ur.getRole().getName()));
//	    }
	    return result;
	  }
	  
	  @Transient
	  public boolean isEnabled() {
	    return true;
	  }

	  @Transient
	  public boolean isAccountNonExpired() {
	    return true;
	  }

	  @Transient
	  public boolean isAccountNonLocked() {
	    return true;
	  }

	  @Transient
	  public boolean isCredentialsNonExpired() {
	    return true;
	  }


}
